<?php
/**
 * @user:andy
 * @date:2021/7/10
 * @company:http://www.sjdphp.com
 * @desc: 通行中间件
 */

namespace app\common\middleware;

use app\common\model\BackCurrent as BackCurrentModel;
class CheckCurrent
{
    public function handle($request,\Closure $next){
        ## 获取当前IP
        $ip = getIp();
        ## 查询IP通行表
        $currentLists = BackCurrentModel::where(array(['current_status','=',config("project.mysql_code.table_normal")],['start_time','<',time()],['end_time','>',time()]))->field("current_ip")->select()->toArray();

        ## 判断是否存在IP
        if(empty($currentLists)){
            return redirect("/index");
        }else{
            ## 判断IP是否存在通行中
            if(!in_array($ip,array_column($currentLists,'current_ip'))){
                return redirect("/index");
            }
        }

        return $next($request);
    }



}